{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c62f6aa4",
   "metadata": {},
   "source": [
    "# 字典知识："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "025f7fa9",
   "metadata": {},
   "source": [
    "## 字典初始知识\n",
    "```\n",
    "列表与字典对比：\n",
    "users = [\n",
    "    [\"xaioming\",18,\"IT\",10000],\n",
    "    [\"xiaoliang\",19,\"IT\",20000]\n",
    "    \n",
    "]\n",
    "# 试对比：\n",
    "users_dict = {\n",
    "    \"xaioming\":[18,\"IT\",10000],\n",
    "    \"xiaoliang\":[19,\"IT\",20000]\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2633a3fc",
   "metadata": {},
   "source": [
    "## 字典创建\n",
    "> 1. info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建\n",
    "> 2. info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建\n",
    "> 3. 运用字典增加的方式新建字典:\n",
    "```\n",
    "方法一：\n",
    "info = {\n",
    "    \"stu10001\":\"xiaoming\",\n",
    "    \"stu10002\":\"lihua\",\n",
    "    \"stu10003\":\"xiaohong\",\n",
    "    \"stu10004\":\"xiaohong\",\n",
    "}\n",
    "print(info)\n",
    "print(type(info))\n",
    "```\n",
    "\n",
    "```\n",
    "方法二：\n",
    "info_02 = dict(\n",
    "    stu10001=\"xiaoming\",\n",
    "    stu10002=\"lihua\",\n",
    "    stu10003=\"xiaohong\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))\n",
    "```\n",
    "\n",
    "```\n",
    "方法三：\n",
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"xiaoming\"\n",
    "info_03[\"stu10002\"] = \"lihua\"\n",
    "info_03[\"stu10003\"] = \"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbab85a9",
   "metadata": {},
   "source": [
    "### 字典的增、删、改、查\n",
    "```\n",
    "1. 字典的增加(和字典的第三种创建方式一致)：\n",
    "info_03 [\"stu10004\"] = \"xiaoliang\"\n",
    "print(info_03)\n",
    "\n",
    "2. 字典的查询\n",
    "* info_03[key_name]\n",
    "* 注意： 正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    "(2.1) key_name in info_03  ## 查看是否存在,如果存在，返回结果为 True ，不存在为 False\n",
    "# 用key查到value的数据\n",
    " info_03[\"stu10003\"]\n",
    "(2.2) info_03[key_name]\n",
    " # 正确的查询字典方式\n",
    "print(\"stu10003\" in info_03)\n",
    "if \"stu10006\" in info_03:\n",
    "    print(info_03[\"stu10006\"])\n",
    "else:\n",
    "    print(\"查询的key不存在\")\n",
    "3. 字典的修改\n",
    "(3.1) 查询key是否存在 \n",
    "(3.2). 修改key的值\n",
    "# 修改: 也要先查询key\n",
    "if \"stu10003\" in info_03:\n",
    "    info_03 [\"stu10003\"] = \"Alex\"\n",
    "print(info_03)\n",
    "例如： 修改info_03中stu10003的值为Alex, if \"stu10003\" in info_03 : info_03 [\"stu10003\"] = \"Alex\"\n",
    "4. 字典的删除\n",
    "(4.1)pop(key)\n",
    "# 字典的删除\n",
    "info_03.pop(\"stu10003\")\n",
    "print(info_03)\n",
    "(4.2).popitem()\n",
    "# 字典的删除 popitem()\n",
    "info_03.popitem()\n",
    "print(info_03)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad80f82f",
   "metadata": {},
   "source": [
    "## 总结：\n",
    "1.观察到字典的特点\n",
    "2.字典创建的三种常见的方法\n",
    "3.字典的增删改查具体的使用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f357fb7f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
